/**
 *
 * @Date: 2018-03-23
 * 功能说明: 从areaSelUtil.js复制而来，重写了树形节点点击事件，
 * 是公有库商品管理模块专用的工具类，实现商品类别和商品品牌选择框联动效果
 *
 * 选择下拉框
 * o 参数对象
 *  webPath:基路径
 *  url:获取数据的url
 *  cid:当前区域id的控件选择器
 *  id:区域id要放入的控件选择器
 *  name:区域名称要放入的控件选择器
 *  activeId:弹出选择框相对控件的位置
 */
function TreeSelUtil(o) {

    this.obj = $.extend({}, o);

    //树形节点的单击事件
    var areaTreeSelClick = function (event, treeId, treeNode, chkFlag) {
        $(com.project.common.web.utils.id).val(com.project.common.web.utils.id);
        $(_obj.name).val(treeNode.name);
        _treeDiv.remove();

        if (_obj.triggerSign){
            // 根据商品类别查询对应的商品品牌
            var categoryId = com.project.common.web.utils.id;
            if (categoryId) {
                $.post('getBrandByCategoryId.jhtml', {"categoryId": categoryId}, function (data) {
                    var result = '<option value="">请选择</option>';
                    $(data).each(function (index, domEle) {
                        result += '<option value="' + domEle.brandId + '">' + domEle.brandName + '</option>';
                    });
                    // 设置HTML内容
                    $('#brandId').html(result);
                })
            } else {
                // 设置HTML内容
                $('#brandId').html('<option value="">请选择</option>');
            }
        }
    };

    var addIdToName = function (temp) {
        var sb = new StringBuffer();
        sb.append(temp.name);
        return sb.toString();
    };

    //数据过滤器
    var filter = function (treeId, parentNode, childNodes) {
        if (!childNodes) return null;
        for (var i = 0, l = childNodes.length; i < l; i++) {
            var temp = childNodes[i];
            childNodes[i].tname = addIdToName(temp);
            delete childNodes[i].url;
        }

        var tempPid = $(_obj.cid).val();

        if (tempPid != null && tempPid != "") {
            for (var m = 0, n = childNodes.length; m < n; m++) {
                if (com.project.common.web.utils.id == tempPid) {
                    childNodes.splice(m, 1);
                    break;
                }
            }
        }

        return childNodes;
    }


    //树形配置
    var setting = {
        async: {
            enable: true,
            url: this.obj.webPath + this.obj.url,
            autoParam: ["com.project.common.web.utils.id"],
            dataFilter: filter,
            type: "get"
        },
        data: {
            key: {
                name: "tname"
            }
        },
        view: {
            dblClickExpand: false
        },
        callback: {
            onClick: areaTreeSelClick
        }
    };

    //获取当前时间，用于一个页面可能存在多个区域选择的需求
    var currentTime = new Date().getTime();

    this.$treeUlId = "treeUl_" + currentTime;
    this.$treeDivId = "treeDiv_" + currentTime;

    //构建div并将div添加到body中
    this.$treeDiv = $('<div id="' + this.$treeDivId + '" class="menuContent" style="display:none; position: absolute;"><ul id="' + this.$treeUlId + '" class="ztree" style="margin-top:0; width:220px;height:300px;"></ul></div>');

    this.$treeDiv.appendTo("body");

    this.$active = $("#" + this.obj.activeId);

    var _obj = this.obj;
    var _treeDiv = this.$treeDiv;

    //body上的绑定事件，用于取消绑定和隐藏区域选择
    var _onBodyDown = function (event) {
        if (!(com.project.common.web.utils.id == _obj.activeId || com.project.common.web.utils.id == _treeDiv.attr("com.project.common.web.utils.id") || $(event.target).parents("#" + _treeDiv.attr("com.project.common.web.utils.id")).length > 0)) {
            _treeDiv.remove();
            $("body").unbind("mousedown", _onBodyDown);
        }
    }

    //显示区域选择并绑定事件
    this.showTree = function () {
        var actOffset = this.$active.offset();
        _treeDiv.css({
            left: actOffset.left + "px",
            top: actOffset.top + this.$active.outerHeight() + "px"
        }).slideDown("fast");
        $("body").bind("mousedown", _onBodyDown);
    };

    //初始化树
    $.fn.zTree.init($("#" + this.$treeUlId), setting);

}

//显示树形选择框
TreeSelUtil.prototype.showTree = function () {
    this.showTree();
};

//树形菜单选择(商品类目)
function ProdCateSelUtil(o) {
    var t = {url: "/intfc/productCategory/getTree.jhtml"};
    return new TreeSelUtil($.extend(t, o));
}

//树形菜单选择(控制系统类目)
function ControlCateSelUtil(o) {
    var t = {url: "/intfc/bitmapControlCatg/getTree.jhtml"};
    return new TreeSelUtil($.extend(t, o));
}